import React,{Component} from 'react'

import{
    View,
    Text,
    StyleSheet,
    Button,
    ToastAndroid,
    Dimensions,
    TextInput,

}from 'react-native'


const { width, height } = Dimensions.get('window');

const styles=StyleSheet.create({
    warp:{
        width:width,
        height:height
    },
    righttext:{
        width:width/2,
        color:"#666",
        fontSize:16,
        borderRadius:4,
    },
    item:{
        width:'100%',
        height:50,
        borderTopWidth:1,
        paddingLeft:10,
        backgroundColor:'white',
        paddingRight:10,
        borderColor:'#ddd',
        flexDirection:'row',
        justifyContent:'space-between',
        alignItems:'center'
    },
})

export default class FoodGuigeAdd extends Component{
    static navigationOptions={
        title:'规格添加',
        headerTitleStyle: {
        alignSelf:'center'
      },
    };


    state={
    name:'',
    sort:'0'
        }

componentWillMount(){
        if(this.props.navigation.state.params){
            this.setState({
                name:this.props.navigation.state.params.name,
                sort:this.props.navigation.state.params.sort.toString()
            })
        }
    }

    saveguige=async function(){
        var id=this.props.navigation.state.params?this.props.navigation.state.params.id:''
        try {
         var data=await fetch('http://www.baomanla.com/fkoufu/a/bossHelper/saveFoodSpecs', {
                method: 'POST',
                 mode : 'cors',
                headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                body: "userid="+global.userdata.id+"&id="+id+"&token="+global.token+"&name="+this.state.name+"&sortValue="+this.state.sort+"&sellerid="+global.sellerid
         })

                
                var result=await data.json()
                if(result.code==1){
                    ToastAndroid.show('添加成功', ToastAndroid.SHORT);
                    this.props.navigation.goBack()
                }
                else{
                    ToastAndroid.show(result.msg, ToastAndroid.SHORT);
                }        
            } catch(error) {
                console.log(error)
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
}


    render(){
        return(
            <View style={styles.warp}>

            <View style={styles.item}>
            <Text style={{color:'black'}}>规格名称</Text>
            <TextInput
            placeholder="请输入规格名称"
            style={styles.righttext}
            defaultValue={this.state.name}
            onChangeText={(text)=>{
                     this.setState({
                         name:text
                     })
                 }}
            />
            </View>

            <View  style={styles.item}>
            <Text style={{color:'black'}}>规格排序</Text>
            <TextInput
            style={styles.righttext}
            defaultValue={this.state.sort}
            onChangeText={(text)=>{
                     this.setState({
                         sort:text
                     })
                 }}
            />
            </View>
            <View style={{marginTop:40}}>
            <Button
                    onPress={()=>{this.saveguige()}}
                    title="添加"
                    style={{padding:20}}
                    color="#841584"
            />
            </View>

            </View>
        )
    }
}